<?php

include_once ("DatabaseManager.php");
include_once ("UserDirectory.php");
include_once ("ZipContainer.php");

session_start();
$user_id = $_SESSION['user_id'];

$db 	= new DatabaseManager();
$con 	= $db->getConnection();

if (!$con)
{
 	die('Could not connect: ' . mysql_error());
}
$db-> createDB();

$action = $_GET['action'];
$active_option = "start";

switch($action)
{
	case "user_choosen":
		$active_option = "kategorien";
		
		if($_POST['button'] == "weiter" )
		  $_SESSION['user_id'] = $_POST['user_id'];
		  
		if($_POST['button'] == "bearbeiten" )
		{
			$_SESSION['user_id'] = $_POST['user_id'];
			$active_option = "start";
		}
		
		if($_POST['button'] == "neuer User" )
			$active_option = "start";
		break;
		
	case "category_choosen":
		$active_option = "vorlagen";
		if($_POST['button'] == "weiter")
			$_SESSION['category_id'] = $_POST['category_id'];
		break;
		
	case "template_choosen":
		$active_option = "bearbeitung";
		
		if($_POST['button'] == "weiter" )
		{
			$_SESSION['template_id'] = intval($_POST['file_id']);
			
			$sql = "SELECT template_name FROM files WHERE file_id = '".$_SESSION['template_id']."' LIMIT 1";
	    $result = mysql_query($sql);
	    $res = mysql_fetch_array($result);
	    
	    $template_name = $res['template_name'];
	        
			$template_dir_path = dirname(__FILE__)."/../users/".$_SESSION['user_id']."/templates/".$_SESSION['category_id']."/".$template_name."/";
			$user_dir_path = dirname(__FILE__)."/../users/".$_SESSION['user_id']."/home/";
			$user_dir = new UserDirectory();
			$user_dir->prepare($user_dir_path, $template_dir_path, $template_name);
			$_SESSION['user_dir'] = $user_dir;
		}
		if($_POST['button'] == "upload template" )
			$active_option = "vorlagen";
		break;
	
	case "template_configured":
		$active_option = "kontrolle";
		$zip_file = new ZipContainer();
		$zip_file->create($_SESSION['user_dir']->getPath(), $_SESSION['user_dir']->getTemplateName());
		$_SESSION['zip'] = $zip_file;
		break;
	
	case "template_checked":
		$active_option = "kontrolle";
		
		if($_POST['button'] == "Mail" )
			$active_option = "kontrolle";
			
		if($_POST['button'] == "Down" )
			$active_option = "kontrolle";
		break;
}

html_start();

echo '<div id="status_bar">';
menu_bar($active_option);
echo "</div>";

echo "<div id='page'>";

switch ($action)
{
	case "user_choosen":
		$user_id = $_POST['user_id'];
		switch($_POST['button'])
		{
			case "bearbeiten":
				include ("ChangeUserData.php");
				break;
			case "neuer User":
				include ("userdata.html");
				break;
			default:
				choose_category();
				break;
		}
	  break;
	
	case "category_choosen":
		$category_id = $_POST['category_id'];
		if($_POST['button'])
			choose_template();
		break;
	
	case "template_choosen":
		$template_id = $_POST['file_id'];
		switch($_POST['button'])
		{
		  case "upload template":
				include("fileupload.html");
				break;
			default:
				config_template();
				break;
		}
		break;
	
	case "template_configured":
		if($_POST['button'])
			check_template();
		break;
	
	case "template_checked":
		switch($_POST['button'])
		{
			case "Mail":
				include ("maildata.html");
				break;
			case "Down":
				include ("download.php");
				break;
			default:
				check_template();
				break;
		}
		break;
	
	default:
		start();
		break;
}
echo "</div>";

echo "<div id='logo'>";
echo "<img src='template-dragon_logo.png' width='250' />";
echo "</div>";

html_end();

function start()
{
	$output = '
	<form name="user_chooser" method="post" action="?action=user_choosen">
	<table>
		<tr>
			<td>%s</td>
			<td>%s</td>
		</tr>
		<tr>
			<td>%s</td>
			<td>%s</td>
		</tr>
	</table><p></p>
	</form>' . "\n";
	
	$sql = "SELECT user_id, firstname, lastname FROM userdata";
	$result = mysql_query($sql);
	
	$user_pulldown = '<select name="user_id" id="user_wahl">';
	
	while($row = mysql_fetch_row($result))
	  $user_pulldown .= '<option value="'.$row[0].'">'.$row[1]." ".$row[2].'</option>';
	
	$user_pulldown .= '</select>';
	$next_button = '<input type="submit" name="button" id="weiter" value="weiter" />';
	$edit_button = '<input type="submit" name="button" id="edit" value="bearbeiten"/>';
	$new_user_button = '<input type="submit" name="button" id="new" value="neuer User"/>';
	
	printf($output, $user_pulldown, $new_user_button, $next_button, $edit_button);
}

function choose_category()
{
	$output = '
	<form name="category_chooser" method="post" action="?action=category_choosen">
	<table>
		<tr>
			<td>%s</td>
			<td>%s</td>
		</tr>
	</table><p></p>
	</form>' . "\n";
	
	$sql = "SELECT category_id, category_name FROM categories";
	$result = mysql_query($sql);
	
	$category_pulldown = '<select name="category_id" id="category wahl">';
	
	while($row = mysql_fetch_row($result))
		$category_pulldown .= '<option value='.$row[0].'>'.$row[1].'</option>';
	
	$category_pulldown .= '</select>';
	$next_button = '<input type="submit" name="button" id="weiter" value="weiter" />';
	
	printf($output, $category_pulldown, $next_button);
}

function choose_template()
{
	$output = '
	<form name="template_chooser" method="post" action="?action=template_choosen">
	<table>
		<tr>
			<td>%s</td>
			<td>%s</td>
		</tr>
		<tr>
			<td>%s</td>
			<td></td>
		</tr>
	</table><p></p>
	</form>' . "\n";
	
	$cat_id = intval($_SESSION['category_id']);
	 
	$sql = "SELECT file_id, template_name FROM files WHERE category_id = '".$cat_id."'";
	$result = mysql_query($sql);
	
	$template_pulldown = '<select name="file_id" id="template wahl">';
	
	while($row = mysql_fetch_row($result))
	  $template_pulldown .= '<option value='.$row[0].'>'.$row[1].'</option>';
	
	$template_pulldown .= '</select>';
	$next_button = '<input type="submit" name="button" id="weiter" value="weiter" />';
	$upload_button = '<input type="submit" name="button" id="upload" value="upload template" />';
	
	printf($output, $template_pulldown, $next_button, $upload_button);
}

function config_template()
{
	$output = '
	<form name="template_configer" method="post" action="?action=template_configured">
	<table>
		<tr>
			<td></td>
			<td>%s</td>
		</tr>
		<tr>
			<td></td>
			<td></td>
		</tr>
	</table><p></p>
	</form>' . "\n";
	
	$next_button = '<input type="submit" name="button" id="weiter" value="weiter" />';
	
	printf($output, $next_button);
}

function check_template()
{
	$output = '
	<form name="template_checker" method="post" action="?action=template_checked">
	<table>
		<tr>
			<td></td>
			<td></td>
		</tr>
		<tr>
			<td>%s</td>
			<td>%s</td>
		</tr>
	</table><p></p>
	</form>' . "\n";
	
	if(!@$title = $_POST["title"])
		$title = 'NULL';
	if(!@$first_name = $_POST["first_name"])
		$first_name = 'NULL';
	if(!@$last_name = $_POST["last_name"])
		$last_name = 'NULL';
	
	$send_button = '<input type="submit" name="button" id="mail_send" value="Mail"/>';
	$down_button = '<input type="submit" name="button" id="zip_down" value="Down"/>';
	
	printf($output, $send_button, $down_button);
}

function menu_bar($active_option = "start")
{
	$menu_link1 = 'START';
	$menu_link2 = 'KATEGORIEN';
	$menu_link3 = 'VORLAGEN';
	$menu_link4 = 'BEARBEITUNG';
	$menu_link5 = 'KONTROLLE';
	
	switch($active_option)
	{
		case "start":
			$menu1 = "active_menu_color";
			$menu2 = "inactive_menu_color";
			$menu3 = "inactive_menu_color";
			$menu4 = "inactive_menu_color";
			$menu5 = "inactive_menu_color";
			$menu_link1 = ' <a href="index.php" ><img src="../pics/start_aktiv.jpg" width=150></a>';		//todo korrekte Links
			$menu_link2 = ' <img src="../pics/kategorien_inaktiv.jpg" width=150></a>';
			$menu_link3 = ' <img src="../pics/vorlagen_inaktiv.jpg" width=150></a>';
			$menu_link4 = ' <img src="../pics/bearbeiten_inaktiv.jpg" width=150></a>';
			$menu_link5 = ' <img src="../pics/kontrolle_inaktiv.jpg" width=150></a>';
			break;
			
		case "kategorien":
			$menu1 = "visited_menu_color";
			$menu2 = "active_menu_color";
			$menu3 = "inactive_menu_color";
			$menu4 = "inactive_menu_color";
			$menu5 = "inactive_menu_color";
			$menu_link1 = '<a href="index.php"><img src="../pics/start_visited.jpg" width=150></a>';		//todo korrekte Links
			$menu_link2 = '<a href="index.php?action=user_choosen"><img src="../pics/kategorien_aktiv.jpg" width=150></a>';
			$menu_link3 = ' <img src="../pics/vorlagen_inaktiv.jpg" width=150></a>';
			$menu_link4 = ' <img src="../pics/bearbeiten_inaktiv.jpg" width=150></a>';
			$menu_link5 = ' <img src="../pics/kontrolle_inaktiv.jpg" width=150></a>';
			break;
			
		case "vorlagen":
			$menu1 = "visited_menu_color";
			$menu2 = "visited_menu_color";
			$menu3 = "active_menu_color";
			$menu4 = "inactive_menu_color";
			$menu5 = "inactive_menu_color";
			$menu_link1 = '<a href="index.php"><img src="../pics/start_visited.jpg" width=150></a>';		//todo korrekte Links
			$menu_link2 = '<a href="index.php?action=user_choosen"><img src="../pics/kategorien_visited.jpg" width=150></a>';
			$menu_link3 = '<a href="index.php?action=category_choosen"><img src="../pics/vorlagen_aktiv.jpg" width=150></a>';
			$menu_link4 = ' <img src="../pics/bearbeiten_inaktiv.jpg" width=150></a>';
			$menu_link5 = ' <img src="../pics/kontrolle_inaktiv.jpg" width=150></a>';
			break;
			
		case "bearbeitung":
			$menu1 = "visited_menu_color";
			$menu2 = "visited_menu_color";
			$menu3 = "visited_menu_color";
			$menu4 = "active_menu_color";
			$menu5 = "inactive_menu_color";
			$menu_link1 = '<a href="index.php"><img src="../pics/start_visited.jpg" width=150></a>';		//todo korrekte Links
			$menu_link2 = '<a href="index.php?action=user_choosen"><img src="../pics/kategorien_visited.jpg" width=150></a>';
			$menu_link3 = '<a href="index.php?action=category_choosen"><img src="../pics/vorlagen_visited.jpg" width=150></a>';
			$menu_link4 = '<a href="index.php?action=template_choosen"><img src="../pics/bearbeiten_aktiv.jpg" width=150></a>';
			$menu_link5 = ' <img src="../pics/kontrolle_inaktiv.jpg" width=150></a>';
			break;
			
		case "kontrolle":
			$menu1 = "visited_menu_color";
			$menu2 = "visited_menu_color";
			$menu3 = "visited_menu_color";
			$menu4 = "visited_menu_color";
			$menu5 = "active_menu_color";
			$menu_link1 = '<a href="index.php"><img src="../pics/start_visited.jpg" width=150></a>';		//todo korrekte Links
			$menu_link2 = '<a href="index.php?action=user_choosen"><img src="../pics/kategorien_visited.jpg" width=150></a>';
			$menu_link3 = '<a href="index.php?action=category_choosen"><img src="../pics/vorlagen_visited.jpg" width=150></a>';
			$menu_link4 = '<a href="index.php?action=template_choosen"><img src="../pics/bearbeiten_visited.jpg" width=150></a>';
			$menu_link5 = '<a href="index.php?action=template_configured"><img src="../pics/kontrolle_aktiv.jpg" width=150></a>';
			break;
	}
	
	$output = '
	<table class="menu">
		<tr>
			<td class ="menu_point"> <div class="' .$menu1 .'">%s<div></td>
			<td class ="menu_arrow"><img src="../pics/pfeil.jpg" width=30></td>
			<td class ="menu_point"> <div class="' .$menu2 .'">%s<div></td>
			<td class ="menu_arrow"><img src="../pics/pfeil.jpg" width=30></td>
			<td class ="menu_point"> <div class="' .$menu3 .'">%s<div></td>
			<td class ="menu_arrow"><img src="../pics/pfeil.jpg" width=30></td>
			<td class ="menu_point"> <div class="' .$menu4 .'">%s<div></td>
			<td class ="menu_arrow"><img src="../pics/pfeil.jpg" width=30></td>
			<td class ="menu_point"> <div class="' .$menu5 .'">%s<div></td>
		</tr>
	</table><p></p>' . "\n";
	
	printf($output, $menu_link1, $menu_link2, $menu_link3, $menu_link4, $menu_link5);
}

function html_start()
{
	echo '
	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
	<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15" />
		<title>Untitled Document</title>
		<style type="text/css">@import "template-dragon_form-style.css";</style>
	</head>
	<body>';
}

function html_end()
{
	echo '
		</body>
	</html>';
}
?>